package main

import (
	"fmt"
	"time"
)

func trackTime() func() {
	start := time.Now()
	defer func() {
		fmt.Println("准备计时...")
	}()
	return func() {
		fmt.Printf("耗时: %v\n", time.Since(start))
	}
}

func main() {
	defer fmt.Println("in main")
	defer fmt.Println("in main2")
	defer func() {
		defer func() {
			panic("panic again and again")
		}()
		panic("panic again")
	}()

	panic("panic once")
}
func panicRecover() {
	defer func() {
		if r := recover(); r != nil {
			fmt.Println("Recovered from panic:", r)
		}
	}()

	panic("触发 panic")
}

func example() {
	defer fmt.Println("第一个 defer") // 第三个执行
	defer fmt.Println("第二个 defer") // 第二个执行
	defer fmt.Println("第三个 defer") // 第一个执行
}

func calc() (result int) {
	defer func() { result *= 2 }() // 修改返回值
	return 5                       // 最终返回 10
}
